package com.vancouver.knows.ui.fragment.homePage;

import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.squareup.picasso.Picasso;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.business.BSharedPrefManager;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.service.impl.HomePageHandle;

public class HomepageBreakNewsView {
	private Activity mActivity;
	private LayoutInflater mInflater;
	@Bind(R.id.relativelayout)
	RelativeLayout layout;
	private View mView;
	@Bind(R.id.break_news)
	TextView mTitle;
	@Bind(R.id.content)
	TextView mContent;
	@Bind(R.id.new_img)
	ImageView newImage;

	@Bind(R.id.view_line)
	View lineView;

	private BusinessModule bModule;
	private HashMap<String, Object> map;
	private List<String> breakNewsList = null;

	public HomepageBreakNewsView(Activity activity, LayoutInflater inflater) {
		mActivity = activity;
		mInflater = inflater;
		findViewsId();
		setViews();
	}

	private void findViewsId() {
		mView = mInflater.inflate(R.layout.break_news_view, null);
		ButterKnife.bind(this, mView);
	}

	private void setViews() {
		setTopLineColor();
		initBreakNewsList();
	}

	@SuppressLint("ResourceAsColor")
	public void setTopLineColor() {
		BSharedPrefManager sharedPrefManager = ((MainApplication) mActivity.getApplication()).getSharedPrefManager();
		if (sharedPrefManager.getUserId() != null) {
			lineView.setBackgroundColor(Color.parseColor("#F05123"));// R.color.line_yellow);
		} else {
			lineView.setBackgroundColor(Color.parseColor("#ff979797"));// R.color.line_color);
		}

	}

	@SuppressWarnings("unchecked")
	public void initBreakNewsList() {
		this.bModule = ((MainApplication) mActivity.getApplication()).getBusinessModule();
		map = bModule.getCacheManager().getHomePageMap();
		if (map == null) {
			return;
		}

		breakNewsList = (List<String>) map.get(HomePageHandle.HOMEPAGE_BREAK_NEWS);
		setBreakNewsInfo();
	}

	@SuppressWarnings("deprecation")
	private void setBreakNewsInfo() {
		if (breakNewsList != null && breakNewsList.size() > 0) {
			mTitle.setText(breakNewsList.get(2));
			mTitle.getPaint().setFakeBoldText(true);// 加粗

			String content = interceptPostContent(breakNewsList.get(3));
			mContent.setText(getReadDetailStyle(content));
			if (breakNewsList.get(0).length() != 0) {
				Picasso.with(mActivity).load(breakNewsList.get(0)).centerCrop()
						.resizeDimen(R.dimen.citynews_item_img_width, R.dimen.citynews_item_img_height).into(newImage);
			} else {
				newImage.setImageDrawable(mContent.getResources().getDrawable(R.drawable.hot_news));
			}
		}
	}

	private Spanned getReadDetailStyle(String content) {
		return Html.fromHtml(content + "..." + "<font color=\"#db6f51\">" + mActivity.getString(R.string.read_detail)
				+ "</font>");
	}

	private String interceptPostContent(String content) {
		if (content.length() < 40)
			return content;
		return content.substring(0, 40);
	}

	@OnClick(R.id.relativelayout)
	public void onNewsClick() {
		if (breakNewsList != null && breakNewsList.size() > 0) {
			Intent intent = new Intent(mActivity, CityNewsDetailActivity.class);
			intent.putExtra("postId", breakNewsList.get(1));
			mActivity.startActivity(intent);

		}
	}

	public View getNewsView() {
		return mView;
	}

	public List<String> getBreakNewsList() {
		return breakNewsList;
	}
}